<template>
	<view :data-theme="themeStyle">
		<mescroll-uni ref="mescroll" @getData="getMemberCounponList">
			<block slot="list">
				<view class="coupon-list">
					<view class="coupon-li" v-for="(item, index) in list" :key="index" @click="liClick(item, index)">
						<image :src="$util.img('upload/uniapp/coupon/coupon_ysy.png')" mode="widthFix"></image>
						<view class="li-top">
							<view class="top-content-left">
								<view class="coupon-name">{{ item.coupon_name }}</view>
								<view class="coupon-desc">
									<text v-if="item.validity_type">领取之日起{{ item.fixed_term }}日内有效</text>
									<text v-else>有效日期至{{ $util.timeStampTurnTime(item.end_time) }}</text>
								</view>
							</view>
							<view class="top-content-right" v-if="item.type == 'reward'">
								<text class="font-big ns-text-color">{{ parseInt(item.money) }}</text>
								<text class="font-sm ns-text-color">元</text>
							</view>
							<view class="top-content-right" v-if="item.type == 'discount'">
								<text class="font-big ns-text-color">{{ parseFloat(item.discount) }}</text>
								<text class="font-sm ns-text-color">折</text>
							</view>
						</view>
						<view class="li-bottom">
							<view class="fonts-sm">
								<!-- <text v-if="item.goods_type==1">全场商品</text>
								<text v-if="item.goods_type!==1">指定商品</text> -->
								<text v-if="item.at_least > 0">满{{ item.at_least }}元可用</text>
								<text v-else>无门槛优惠券</text>
								<text class="ns-text-color" v-if="item.discount_limit != '0.00'">(最大优惠{{ item.discount_limit }}元)</text>
							</view>
							<view class="li-bottom-right">
								<view class="getCoupon ns-text-color" v-if="item.useState == 0" @click="receiveCoupon(item, index)">立即领取</view>
								<view class="getCoupon ns-text-color" v-if="item.useState == 1" @click="toGoodsList(item, index)">去使用</view>
								<view class="iconfont iconright ns-text-color" v-if="item.useState != 2"></view>

								<view class="getCoupon ns-text-color-gary" v-if="item.useState == 2">去使用</view>
								<view class="iconfont iconright ns-text-color-gary" v-if="item.useState == 2"></view>
							</view>
						</view>
					</view>
				</view>

				<view v-if="list.length == 0"><ns-empty text="当前没有可以领取的优惠券哦!" :isIndex="!1"></ns-empty></view>
			</block>
		</mescroll-uni>
		<loading-cover ref="loadingCover"></loading-cover>
		<ns-show-toast></ns-show-toast>
	</view>
</template>

<script>
import globalConfig from '@/common/js/golbalConfig.js';
import nsShowToast from '@/components/ns-show-toast/ns-show-toast.vue';
export default {
	data() {
		return {
			list: [],
			couponBtnSwitch: false
		};
	},
	components: {
		nsShowToast
	},
	onShow() {
		// 刷新多语言
		this.$langConfig.refresh();
		// if (this.$refs.mescroll) this.$refs.mescroll.refresh(false);
	},
	mixins: [globalConfig],
	methods: {
		liClick(item, index) {
			if (item.useState == 0) this.receiveCoupon(item, index);
			else this.toGoodsList(item, index);
		},

		//领取优惠券
		receiveCoupon(item, index) {
			if (this.couponBtnSwitch) return;
			this.couponBtnSwitch = true;
			let token = uni.getStorageSync('token');
			if (token != '') {
				this.$api.sendRequest({
					url: '/coupon/api/coupon/receive',
					data: {
						coupon_type_id: item.coupon_type_id,
						get_type: 2 //获取方式:1订单2.直接领取3.活动领取
					},
					success: res => {
						this.couponBtnSwitch = false;
						let msg = '';
						msg = '领取成功，快去使用吧';
						let list = this.list;
						if (res.data.is_exist == 1 && res.code < 0) {
							msg = '您已领取过该优惠券，快去使用吧';
						}
						if (res.data.is_exist == 1) {
							for (let i = 0; i < list.length; i++) {
								if (list[i].coupon_type_id == item.coupon_type_id) {
									list[i].useState = 1;
								}
							}
						} else {
							for (let i = 0; i < list.length; i++) {
								if (list[i].coupon_type_id == item.coupon_type_id) {
									list[i].useState = 2;
								}
							}
						}
						this.$util.showToast({
							title: msg
						});
					},
					fail: res => {
						this.couponBtnSwitch = false;
					}
				});
			} else {
				this.couponBtnSwitch = true;
				this.$util.redirectTo('/pages/login/login/login');
			}
		},
		//获取优惠券列表
		getMemberCounponList(mescroll) {
			this.$api.sendRequest({
				url: '/coupon/api/coupon/typepagelists',
				data: {
					page: mescroll.num,
					page_size: mescroll.size
				},
				success: res => {
					let newArr = [];
					let msg = res.message;
					if (res.code == 0 && res.data) {
						newArr = res.data.list;
					} else {
						this.$util.showToast({
							title: msg
						});
					}
					mescroll.endSuccess(newArr.length);
					if (newArr.length) {
						newArr.forEach(v => {
							v.useState = 0;
						});
					}
					//设置列表数据
					if (mescroll.num == 1) this.list = []; //如果是第一页需手动制空列表
					this.list = this.list.concat(newArr); //追加新数据
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail() {
					mescroll.endErr();
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},
		imageError(index) {
			this.list[index].logo = this.$util.getDefaultImage().default_shop_img;
			this.$forceUpdate();
		},
		couponImageError(index) {
			this.list[index].image = this.$util.img('upload/uniapp/goods/coupon.png');
			this.$forceUpdate();
		},
		toGoodsList(item) {
			if (item.goods_type != 1) {
				this.$util.redirectTo('/pages/goods/list/list', {
					coupon: item.coupon_type_id
				});
			} else {
				this.$util.redirectTo('/pages/goods/list/list', {});
			}
		}
	},
	/**
	 * 自定义分享内容
	 * @param {Object} res
	 */
	onShareAppMessage(res) {
		var title = '送您一张优惠券,快来领取吧';
		var path = '/otherpages/goods/coupon/coupon';
		return {
			title: title,
			path: path,
			success: res => {},
			fail: res => {}
		};
	}
};
</script>

<style lang="scss">
.coupon-list {
	width: 100%;
	height: 100%;
	padding: $ns-padding;
	box-sizing: border-box;
	background: #f5f5f5;

	.coupon-li {
		width: 702rpx;
		height: 206rpx;
		margin: 0 auto;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-bottom: $ns-margin;
		position: relative;

		image {
			width: 100%;
			height: 100%;
			position: absolute;
			left: 0;
			top: 0;
		}

		.li-top {
			width: 652rpx;
			margin: 0 auto;
			display: flex;
			justify-content: space-between;
			align-items: flex-end;
			border-bottom: 1px dashed #e1e1e1;
			position: relative;
			margin-top: 39rpx;
			line-height: 1;
			padding: 0 10rpx;
			box-sizing: border-box;
			padding-bottom: 26rpx;

			.top-content-left {
				display: flex;
				flex-direction: column;
				line-height: 1;
				flex: 1;
				overflow: hidden;

				.coupon-name {
					width: 100%;
					line-height: 1;
					color: #000;
					font-size: $ns-font-size-lg;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.coupon-desc {
					line-height: 1;
					color: #ababab;
					font-size: $ns-font-size-x-sm;
					margin-top: 23rpx;
				}
			}

			.top-content-right {
				display: flex;
				justify-content: center;
				align-items: center;
				line-height: 1;

				.font-sm {
					line-height: 1;
					margin-top: 20rpx;
					font-size: $ns-font-size-lg;
					margin-right: 6rpx;
				}

				.font-big {
					line-height: 1;
					font-size: 70rpx;
				}
			}
		}

		.li-bottom {
			flex: 1;
			width: 652rpx;
			margin: 0 auto;
			display: flex;
			justify-content: space-between;
			align-items: center;
			z-index: 5;
			padding: 0 10rpx;
			box-sizing: border-box;

			.fonts-sm {
				color: #ababab;
				font-size: $ns-font-size-sm;
			}

			.li-bottom-right {
				display: flex;

				.getCoupon {
					font-size: $ns-font-size-base;
				}

				.iconright {
					margin-left: 10rpx;
					font-size: $ns-font-size-base;
				}
			}
		}
	}
}

.empty {
	margin-top: 200rpx;
}
</style>
